﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAPbobsCOM;
using System.IO;

namespace CompanySyncProject
{
    class Program
    {
        static public SAPbobsCOM.Company oCompany = null;
        const string logFile = @"log\logs.txt";
        static System.IO.StreamWriter strWriter;
        static string nowStr = string.Empty;
        static string imexType = string.Empty;
        static string logpath = @"log";
        static void Main(string[] args)
        {
            DirectoryInfo di = new DirectoryInfo(logpath);
            if (di.Exists == false)
                Directory.CreateDirectory(logpath);

            FileInfo fi = new FileInfo(logFile);
            if (fi.Exists == false)
                File.Create(logFile);

            strWriter = new StreamWriter(logFile);

            DateTime now = DateTime.Now;
            nowStr = (now.Year.ToString() + now.Month.ToString("00") + now.Day.ToString("00") + now.Hour.ToString("00") + now.Minute.ToString("00") + now.Second.ToString("00"));

            oCompany = new SAPbobsCOM.Company();
            /*
            oCompany.Server = @"localhost\SQL2012";
            oCompany.LicenseServer = "localhost:30000";
            oCompany.CompanyDB = "CosineMRPTest";
            oCompany.language = SAPbobsCOM.BoSuppLangs.ln_English;
            oCompany.UseTrusted = false;
            oCompany.DbUserName = "sa";
            oCompany.DbPassword = "@sap.c0m";
            oCompany.UserName = "manager";
            oCompany.Password = "1111";
            oCompany.DbServerType = SAPbobsCOM.BoDataServerTypes.dst_MSSQL2012;
            */

            oCompany.Server = @"DESKTOP-COSINEH";
            oCompany.LicenseServer = "localhost:30000";
            oCompany.CompanyDB = "CosineTest001";
            oCompany.language = SAPbobsCOM.BoSuppLangs.ln_English;
            oCompany.UseTrusted = false;
            oCompany.DbUserName = "sa";
            oCompany.DbPassword = "@sap.c0m";
            oCompany.UserName = "manager";
            oCompany.Password = "1111";
            oCompany.DbServerType = SAPbobsCOM.BoDataServerTypes.dst_MSSQL2014;


            try
            {
                System.Console.WriteLine("before connection");
                oCompany.Connect();
            }
            catch (Exception e)
            {
                string t = e.Message;
                System.Console.WriteLine(t);
            }
            int Ret;
            string msg;
            oCompany.GetLastError(out Ret, out msg);
            System.Console.WriteLine(msg);
            System.Console.WriteLine("after connection");

            if (Ret == 0 && msg == string.Empty)
            {
                oCompany.XmlExportType = BoXmlExportTypes.xet_ExportImportMode;
                string errMsg = string.Empty;

                System.Console.WriteLine("before start transaction");
                oCompany.StartTransaction();
                System.Console.WriteLine("after start transaction");
                //BaseRun run = new MarketingDoc(oCompany, strWriter);
                //BaseRun run = new DIOrderService(oCompany, strWriter);
                //BaseRun run = new DIItem(oCompany, strWriter);
                //BaseRun run = new DIStockTransfer(oCompany, strWriter);
                //BaseRun run = new DIPayment(oCompany, strWriter);
                //BaseRun run = new DISapBobs(oCompany, strWriter);
                //BaseRun run = new DIUdo(oCompany, strWriter);
                //BaseRun run = new DIGoodsReceipt(oCompany, strWriter);
                //BaseRun run = new DIGoodsReceipt(oCompany, strWriter);
                //BaseRun run = new DICancelMarketingDoc(oCompany, strWriter);
                BaseRun run = new DIOpportunity(oCompany, strWriter);
                System.Console.WriteLine("before run");
                run.Run();
                System.Console.WriteLine("after run");
                errMsg = oCompany.GetLastErrorDescription();
                if (oCompany.InTransaction == true)
                {
                    //oCompany.EndTransaction(BoWfTransOpt.wf_RollBack);
                    oCompany.EndTransaction(BoWfTransOpt.wf_Commit);
                }
                System.Console.WriteLine(errMsg);
                oCompany.Disconnect();

                strWriter.Close();
                string targetFile = String.Format(@"c:\temp\log\log-{0}.txt", nowStr);
                //File.Copy(logFile, targetFile, true);
                System.Console.WriteLine("Done");
                
            }
            else
            {
                System.Console.WriteLine("cannot connect to company");
            }
            
        }
    }
}
